
package com.borqs.music.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.borqs.music.R;
import com.borqs.music.util.SharedPreferencesCompat;

public class SignupUploadActivity extends Activity {
    public static final String PREFERENCE_KEY_SIGNUP_UPLOAD = "sign_up_upload";
    
    public static final int AUTO_UPLOAD_BOTH = 0;
    public static final int AUTO_UPLOAD_WIFI = 1;
    public static final int AUTO_UPLOAD_DISABLE = 2;
    
    private Button close_ui;
    private RadioGroup mRadioGroup;
    private SharedPreferences mPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup_upload_activity);
        setTitle(R.string.signup_application_upload_title);

        mRadioGroup = (RadioGroup) findViewById(R.id.upload_radio_group);
        String text = getText(R.string.signup_application_upload_radio_1).toString();
        SpannableStringBuilder style = new SpannableStringBuilder(text);
        style.setSpan(new ForegroundColorSpan(Color.RED), text.indexOf("(") + 1,
                text.indexOf(")"), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        ((RadioButton) mRadioGroup.getChildAt(0)).setText(style);
        mPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        final int uploadSettings = mPreferences.getInt(PREFERENCE_KEY_SIGNUP_UPLOAD, -1);
        if (uploadSettings < AUTO_UPLOAD_BOTH || uploadSettings > AUTO_UPLOAD_DISABLE) {
            mRadioGroup.check(mRadioGroup.getChildAt(AUTO_UPLOAD_WIFI).getId());
        } else {
            mRadioGroup.check(mRadioGroup.getChildAt(uploadSettings).getId());
        }

        close_ui = (Button) findViewById(R.id.close_button);
        close_ui.setText(android.R.string.ok);

        close_ui.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                saveCurrentOption();
                finish();
            }
        });
    }

    public static void selfCheck(Context context) {
        SharedPreferences prefer = PreferenceManager.getDefaultSharedPreferences(context);
        int uploadSettings = prefer.getInt(PREFERENCE_KEY_SIGNUP_UPLOAD, -1);
        if (uploadSettings < AUTO_UPLOAD_BOTH || uploadSettings > AUTO_UPLOAD_DISABLE) {
            Intent intent = new Intent(context.getApplicationContext(), SignupUploadActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }

    public static boolean isAbleToUpload(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeInfo = cm.getActiveNetworkInfo();
        if (null == activeInfo) {
            // no available network.
            return false;
        }
        SharedPreferences prefer = PreferenceManager.getDefaultSharedPreferences(context);
        int uploadSettings = prefer.getInt(PREFERENCE_KEY_SIGNUP_UPLOAD, -1);
        boolean ret = false;
        if (AUTO_UPLOAD_BOTH == uploadSettings) {
            ret = true;
        } else if (AUTO_UPLOAD_WIFI == uploadSettings) {
            ret = activeInfo.getTypeName().equalsIgnoreCase("WIFI");
        }
        return ret;
    }

    private void saveCurrentOption() {
        final int checkedId = mRadioGroup.getCheckedRadioButtonId();
        final int index = mRadioGroup.indexOfChild(findViewById(checkedId));
        
        Editor ed = mPreferences.edit();
        ed.putInt(PREFERENCE_KEY_SIGNUP_UPLOAD, index);
        SharedPreferencesCompat.apply(ed);

        Intent resultData = new Intent();
        resultData.putExtra("IS_ENABLE", (index != AUTO_UPLOAD_DISABLE));
        setResult(RESULT_OK, resultData);
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_BACK) {
             saveCurrentOption();
          }
         return super.onKeyDown(keyCode, event);
    }
}
